﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using Toolkit.Web.ExtensionMethods;

namespace Toolkit.Web.Mvc.ExtensionMethods
{
    public static class RouteDataExtensions
    {
        /// <summary>
        /// Returns whether the current request was routed through the given controller name (ignoring the action).
        /// </summary>
        /// <param name="routeData"></param>
        /// <param name="controllerName"></param>
        /// <returns></returns>
        public static bool IsRoutedFrom(this RouteData routeData, string controllerName)
        {
            if (routeData == null)
            {
                throw new ArgumentNullException();
            }

            if (string.IsNullOrEmpty(controllerName))
            {
                throw new ArgumentException();
            }

            return routeData.GetRequiredString("controller").IsCaseInsensitiveEqual(controllerName);
        }

        /// <summary>
        /// Returns whether the current request was routed through the given controller and action names.
        /// </summary>
        /// <param name="routeData"></param>
        /// <param name="controllerName"></param>
        /// <param name="actionName"></param>
        /// <returns></returns>
        public static bool IsRoutedFrom(this RouteData routeData, string controllerName, string actionName)
        {
            if (routeData == null)
            {
                throw new ArgumentNullException();
            }

            if (string.IsNullOrEmpty(controllerName))
            {
                throw new ArgumentException();
            }

            if (string.IsNullOrEmpty(actionName))
            {
                throw new ArgumentException();
            }

            return (
                routeData.GetRequiredString("controller").IsCaseInsensitiveEqual(controllerName) &&
                routeData.GetRequiredString("action").IsCaseInsensitiveEqual(actionName)
            );
        }
    }
}
